<?php

function send_mail($from_email, $to_email, $subject, $content, $isHMTL=false)
{
	if ( empty($to_email) ) return null;
	
	if ( is_array($from_email) )
	{
		$from_email = $from_email['name'].' <'.$from_email['email'].'>';
	}
	
	$headers = 'From: '.$from_email."\r\n".
		'Reply-To: '.$from_email."\r\n".
		'X-Mailer: PHP/'.phpversion()."\r\n";
	if ( $isHMTL )
	{
		$headers .= 'Content-type:text/html;charset=utf-8'."\r\n";
	}
	
	if ( mail($to_email, $subject, $content, $headers) )
		return TRUE;
	else 
		return FALSE;
}

function write_file($file, $content, $path=null)
{
	$path = empty($path)?dirname(__FILE__):$path;
	
	$fp = fopen($path.'/'.$file, 'a');
	if($fp !== FALSE){
		fwrite($fp, $content."\n\n");
		fclose($fp);
	}
	unset($fp);
}

function dz2punbb_bbcode($str)
{
	if ( !empty($str) )
	{
		$search = array 
		(
			// Style
			'/\[size=\d\](.*)\[\/size\]/',			// font size
			'/\[hide\](.*)\[\/hide\]/',				// hide words
			'/\[attach\](.*)\[\/attach\]/',			// attach file
			'/\[code="\w+"\]/',						// custom code
			'/\[font=[\w|\s]+\]/',					// font family for English
			'/\[font=[\x{2E80}-\x{9FFF}]+\]/u',		// font family for Chinses
			'/\[\/font\]/',							// font shut
			'/\[\*\]/',	
			'/\[\/list\]/',
			'/\[list\]\[\/\*\]/',
			'/\[i=s\]/',
		);

		$replace = array 
		(
			'$1',
			'$1',
			'附件信息失效',
			'[code]',
			'',
			'',
			'',
			'[/*][*]',
			'[/*][/list]',
			'[list]',
			'[i]',
		);
	}
	return trim(preg_replace($search, $replace, $str));
}

function dz2babel_bbcode($str)
{
	if ( !empty($str) )
	{
		$search = array 
		(
			'/\[size=\d\](.*)\[\/size\]/',			// font size
			'/\[hide\](.*)\[\/hide\]/',				// hide words
			'/\[attach\](.*)\[\/attach\]/',			// attach file
			'/\[code="\w+"\]/',						// custom code
			'/\[code=\]/',						// custom code
			'/\[font=[\w|\s]+\]/',					// font family for English
			'/\[font=[\x{2E80}-\x{9FFF}]+\]/u',		// font family for Chinses
			'/\[\/font\]/',							// font
			'/\[color=[#]{0,1}\w+\]/',				// color
			'/\[\/color\]/',						// color
			'/\[list=\w+\]/',	
			'/\[\*\]/',
			'/\[\/list\]/',
			'/\[list\]\[\/\*\]/',
			'/\[i=s\]/',
		);

		$replace = array 
		(
			'$1',
			'$1',
			'[b]附件信息失效[/b]',
			'[code=$1]',
			'[code]',
			'',
			'',
			'',
			'',
			'',
			'',
			'',
			'',
			'',
			'[i]',
		);
	}
	return trim(preg_replace($search, $replace, $str));
}

// Generates a salted, SHA-1 hash of $str
function password_hash($str, $salt)
{
	return sha1($salt.sha1($str));
}

// Generate a random key of length $len
function random_key($len, $readable = false, $hash = false)
{
	$key = '';

	if ($hash)
	{
		$key = substr(sha1(uniqid(rand(), true)), 0, $len);
	}
	else if ($readable)
	{
		$chars = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789';

		for ($i = 0; $i < $len; ++$i)
			$key .= substr($chars, (mt_rand() % strlen($chars)), 1);
	}
	else
	{
		for ($i = 0; $i < $len; ++$i)
			$key .= chr(mt_rand(33, 126));
	}
	
	return $key;
}

function escape($str)
{
	if (is_array($str))
		return '';

	return mysql_escape_string($str);
}

?>